package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.CoverFaultRepository;
import com.hualude.roadsign.repository.PushHistoryRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.CoverFaultCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/8/11.
 */
@Service
public class CoverFaultCheckServiceImpl implements CoverFaultCheckService {

    @Resource
    private CoverFaultRepository coverFaultRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getCoverFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTCoverFaultDto> dto4DTs = coverFaultRepository.getCoverFault4DT(dtRequestDto, actorId, orgId);
        Integer count = coverFaultRepository.getCoverFaultCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTCoverFaultDto dtMachineFaultDto : dto4DTs) {
            String creatorName = userRepository.getUserNameByUserId(dtMachineFaultDto.getCoverFaultCreatorId());
            dtMachineFaultDto.setCoverFaultCreatorName(creatorName);
            String checkerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getCoverCheckerId());
            dtMachineFaultDto.setCoverCheckerName(checkerName);
            if (dtMachineFaultDto.getCoverMainCheckerId() != null) {
                String mainCheckerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getCoverMainCheckerId());
                dtMachineFaultDto.setCoverMainCheckerName(mainCheckerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer coverFaultAudit(Integer isPass, String feedback, Integer coverFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        Integer check = coverFaultRepository.getMachineCheck(coverFaultId);
        //check:1,主管在审核
        check = (isPass == 2) ? 1 : check;
        if (check == 1) {
            if (isPass == 1 || isPass == 2) {
                final String[] strFaultType = {"损毁", "其他"};
                final CoverFaultDto faultDto = coverFaultRepository.getInfoByFaultId(coverFaultId);
                final Integer orgId = faultDto.getCoverOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "窨井 NO：" + faultDto.getCoverId() + " " +
                        strFaultType[faultDto.getCoverFaultType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),9);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                if (isPass == 1)
                    return coverFaultRepository.coverFaultAudit2Success(isPass, feedback, coverFaultId, userId, dateNowStr);
                else
                    return coverFaultRepository.straightFaultAudit(isPass, feedback, coverFaultId, userId, dateNowStr);

            } else {
                return coverFaultRepository.coverFaultAudit2Fail(isPass, feedback, coverFaultId, userId, dateNowStr);
            }
        } else {
            if (isPass == 1) {
                final CoverFaultDto faultDto = coverFaultRepository.getInfoByFaultId(coverFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getCoverMainOrgId(), 7);
                final String msg = "窨井 NO：" + faultDto.getCoverId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getCoverMainOrgId(), 7, msg, formatter.format(new Date()),9);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return coverFaultRepository.coverFaultAudit(isPass, feedback, coverFaultId, userId, dateNowStr);
        }
    }

    public List<CoverFaultDto> getCoverFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                          Integer count, Integer size) throws Exception {
        return coverFaultRepository.getCoverFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

}
